<?php


namespace app\api\model;


use think\Cache;
use think\Model;

class User extends Model
{

    // /**
    //  * 性别获取器
    //  *
    //  */
    // public function getsexAttr($value)
    // {
    //
    //     $status = [1 => '男', 2 => '女'];
    //
    //     return $status[$value];
    // }
    //
    // /**
    //  * 学历获取器
    //  *
    //  */
    // public function getxueliAttr($value)
    // {
    //     $status = [0 => '小学', 1 => '初中', 2 => '高中', 3 => '专科', 4 => '本科', 5 => '研究生'];
    //
    //     return $status[$value];
    // }

    /**
     * 关联地区表
     *
     * @param string $token
     * @return false
     * @throws \think\exception\DbException
     */

    public function region()
    {
        return $this->hasOne('Region', 'region_id');
    }


    /**
     * 用戶登录
     *
     */
    public function login($post)
    {
        $username = $post['phone'];
        $password = md5(sha1($post['password']));
        $res = $this->where('username', $username)->find()->toArray();
        if ($res == NULL) {
            return json(['code' => 401, 'msg' => '该手机号未注册']);
        }
        $res = $this->where('username', $username)->find()->toArray();
        if ($password != $res['password']) {
            return json(['code' => 401, 'msg' => '密码错误,请重试']);
        } else {
            $token = $this->token($res['id']);
            var_dump($token);
            Cache::set($token,86400 * 7);
            return $res;
        }

    }
    /**
     * 获取token
     * @return mixed
     */
    public function getToken()
    {
        var_dump($this->token);
        return $this->token;
    }

    /**
     *  获取用户个人信息
     */
    public function getUserinfo($id)
    {

        $info = $this->where('id', $id)->find()->toArray();
        $fanids = explode(",", $info['fans']);
        $guanzhus = explode(",", $info['guanzhu']);
        $gcount = count($guanzhus);
        $fcount = count($fanids);
        $info['fans_num'] = $fcount;
        $info['guanzhu_num'] = $gcount;
        return $info;
    }

    /**
     *
     * 用户粉丝数
     */

    public function getFan($id)
    {
        $fanids = $this->where('id', $id)->field('fans')->find()->toArray();
        $ids = explode(",", $fanids['fans']);
        $list = [];
        foreach ($ids as $values) {
            $id = $values;
            $res = $this->where('id', $id)->find();
            $list = $this->where('id', $res['id'])->find();
            var_dump($list);
        }
        return $list;
    }

    /**
     *
     * 用户关注数
     */

    public function getGuanzhu($id)
    {
        $follows = $this->where('id', $id)->field('guanzhu')->find()->toArray();
        $ids = explode(",", $follows['guanzhu']);
        foreach ($ids as $values) {
            $id = $values;
            $res = $this->where('id', $id)->find();
            var_dump($res['id']);
            $list = $this->where('id', $res['id'])->find();
        }
        return json_decode($list);
    }

    /**
     * 修改用户信息
     *
     */
    public function updUser($id, $data)
    {
        $res = $this->where('id', $id)->update($data);
        return $res;
    }

    /**
     * 生成用户认证的token
     * @param $openid
     * @return string
     */
    private function token($id)
    {
        // 生成一个不会重复的随机字符串
        $guid = \getGuidV4();
        // 当前时间戳 (精确到毫秒)
        $timeStamp = microtime(true);
        // 自定义一个盐
        $salt = 'token_salt';
        return md5("{$timeStamp}_{$guid}_{$salt}");
    }
}